const app = getApp()
var WxParse = require('../../wxParse/wxParse.js');
// pages/world/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    readcount:1,
    height: 0,
    detail:{},
    article:'',
    dataList:[],
    isShowLogin: false,
    scimg:"/images/sc.png",//收藏状态图
    issc:0//是否收藏表示
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
      readcount: options.readcount
    })
    var that = this;
    //检测登录
    if (app.globalData.userInfo==null){
      console.log("没用户数据");
      this.userLogin();
      setTimeout(function(){
        that.getIsCollected();
      },2500)
    }else{
      this.getIsCollected();
    }
    this.getSystermMsg();
   
    this.requestDataList();
    //this.textrequestDataList();
  },
  //检测登录
  userLogin: function () {
    // 查看是否授权
    var that = this;
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {

              that.setData({
                userInfo: res.userInfo
              })
              app.wxLogin(res.userInfo);
            }
          })
        } else {

          that.setData({
            isShowLogin: true
          })
        }
      }
    })
  },
  bindGetUserInfo: function (e) {
    var that = this;
    if (e.detail.userInfo == undefined) {
      //用户拒绝授权进行处理
      wx.showModal({
        title: '提示',
        content: '您点击了拒绝授权,将无法正常使用,点击确定重新获取授权。',
        success: function (res) {

        }
      })
    } else {
      //授权成功
      this.setData({
        isShowLogin: false
      })
      wx.getUserInfo({
        success: function (res) {

          that.setData({
            userInfo: res.userInfo
          })

        }
      })
      app.wxLogin(e.detail.userInfo);
    }
  },


  //获取屏幕高度，宽度
  getSystermMsg: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height: res.windowHeight
        })
      }
    })
  },

  getIsCollected:function(){
    
    if (app.globalData.userInfo == null) {
      console.log("无登录状态");
      this.userLogin();
      return false;
    }
    var that = this;
   
    wx.request({
      url: app.appSetting.host + "world/IsCollected?worldId=" + this.data.id + "&userId=" + app.globalData.userInfo.UserId,
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {

        wx.hideLoading();
        if (res.data.Status == 0) {
          //res.data.Result.Detail = app.convertHtmlToText(res.data.Result.Detail);
          if (res.data.Result == 0) {
            that.setData({
              issc: 0,
              scimg: "/images/sc.png"
            });
          } else {
            that.setData({
              issc: 1,
              scimg: "/images/sced.png"
            });
          }



        } else {
          app.showMsg(res.data.Message);
        }

      }
    })
  },

  addReadCount:function(){
   
    wx.request({
      url: app.appSetting.host + "world/AddReadCount?worldId=" + this.data.id ,
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {

      }
    })
  },

  collect:function(){
    if (app.globalData.userInfo==null){
      console.log("无登录状态");
      this.userLogin();
      return false;
    }
    var that = this;
    wx.showLoading({
      title: '处理中.',
    })
    wx.request({
      url: app.appSetting.host + "world/Collect?worldId=" + this.data.id + "&userId=" + app.globalData.userInfo.UserId,
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        
        wx.hideLoading();
        if (res.data.Status == 0) {
          //res.data.Result.Detail = app.convertHtmlToText(res.data.Result.Detail);
            if(that.data.issc==1){
              that.setData({
                issc: 0,
                scimg: "/images/sc.png"
              });
            }else{
              that.setData({
                issc: 1,
                scimg: "/images/sced.png"
              });
            }
         
          

        } else {
          app.showMsg(res.data.Message);
        }

      }
    })
  },

  requestDataList: function () {
    var that = this;
    wx.showLoading({
      title: '读取中 ',
    })
    wx.request({
      url: app.appSetting.host + "world/GetWorldDetail?id=" + this.data.id,
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
       
        wx.hideLoading();
        if (res.data.Status == 0) {
          //res.data.Result.Detail = app.convertHtmlToText(res.data.Result.Detail);

          WxParse.wxParse('article', 'html', res.data.Result.Detail, that, 5);
          that.setData({
            detail: res.data.Result,
            dataList: res.data.Result.CommondList
          });
        
          wx.setNavigationBarTitle({
            title: res.data.Result.Title
          })
        } else {
          app.showMsg(res.data.Message);
        }

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showShareMenu({
      withShareTicket: true
    })
    this.addReadCount();
  },
  shareResult:function(){
    wx.showShareMenu({
      withShareTicket: true
    })
  },
  gotoindex:function(){
    console.log(1);
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  onShareAppMessage: function (res) {
    var that = this;
    console.log(that.data.detail);
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: that.data.detail.Title,
      path: '/pages/world/detail?id=' + that.data.id + '&readcount=' + that.data.readcount,
      imageUrl:that.data.detail.ImgUrl,
      success: function (res) {
        //getSystemInfo是为了获取当前设备信息，判断是android还是ios，如果是android
        //还需要调用wx.getShareInfo()，只有当成功回调才是转发群，ios就只需判断shareTickets
        wx.getSystemInfo({
          success: function (d) {
            // console.log(d.platform);
            //console.log(res.shareTickets);
            if (res.shareTickets == null) {

              return;
            }
            if (d.platform == 'android' && (res.shareTickets.length > 0)) {
              wx.getShareInfo({
                shareTicket: res.shareTickets,
                success: function () {
                  //console.log("安卓群");
                  //app.sharePage();
                  wx.showToast({
                    title: "分享成功",
                    duration: 2000
                  });
                },
                fail: function () {

                  wx.showToast({
                    title: "分享成功",
                    duration: 2000
                  });
                }
              })
              return;
            }
            if (d.platform == 'ios' && (res.shareTickets.length > 0)) {

              //console.log("IOS群");
              //app.sharePage();
              wx.showToast({
                title: "分享成功",
                duration: 2000
              });
            } else {
              wx.showToast({
                title: "分享成功",
                duration: 2000
              });
            }
          },
          fail: function (res) {
            wx.showToast({
              title: "转发失败",
              duration: 2000
            });
          }
        })
      },
      fail: function (res) {
        // 转发失败
        console.log("取消转发");
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})